﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace BadassPhotoAlbum
{
    /**************************************************************************************
     * Created by Mike Flood, Adam Reaves and Joseph Alfred
     * This Class is the main class for the user interface.This class manages the selected
     * album and photo and passes that information to the related functions. It also keeps
     * track of the thumbnail list and the size of the thumbnail.
     * Update: Main Form is no longer used. ControlForm is now Main Form. 
     * Updated By: Steven Gonzalez & Adam Reaves 12/06/2011 
     * ***********************************************************************************/
    public partial class MainForm : Form
    {
        private Interface mainUserInterface;
        private int selectedAlbumIndex = -1;
        private Album selectedAlbum = null;
        private const int THUMBNAILSIZE = 100; // the max size, width and height of thumbnails
        private Mutex addPhotoMutex;
        private ImageList thumbnailsList = null;


        public MainForm()
        {
            InitializeComponent();
            this.Height = 612;
            this.Width = 919;
            mainUserInterface = new Interface();
            albumListView.Parent = albumViewGroupPanel;
            albumListView.Visible = true;
            addAlbumButton.Parent = albumViewGroupPanel;// todo add dock
            slideshowbutton.Parent = albumViewGroupPanel;
            albumNameTextbox.Parent = albumViewGroupPanel;
            albumDescriptionTextBox.Parent = albumViewGroupPanel;
            albumViewGroupPanel.BringToFront();
            albumViewGroupPanel.Visible = true;
           
            viewThumbnailsPanel.Parent = primaryPanel;

            editAlbumPanel.Parent = primaryPanel;
            
            editPhotoPictureBox.Parent = editPhotoPanel;
           
            editPhotoPanel.Parent = primaryPanel;

            editPhotoPanel.Location = new Point(205, 0);
            editPhotoPanel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;

            viewThumbnailsPanel.Location = new Point(205, 0);
            viewThumbnailsPanel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;

            editAlbumPanel.Location = new Point(205, 0);
            editAlbumPanel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;

            newAlbumEntryPanel.Location = new Point(205, 0);
            newAlbumEntryPanel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;

            this.thumbnailListView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyDownDeletePhoto);
            this.albumListView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyDownDeleteAlbum);

            //handler for double click
            thumbnailListView.DoubleClick += thumnailListView_DoubleClick;

            albumListView.ItemSelectionChanged += albumSelectedView;
            albumListView.MouseUp += checkForNoAlbumListViewIndicies;

            //Drag and Drop handlers
            thumbnailListView.ItemDrag += StartDragEvent;
            albumListView.AllowDrop = true;
            albumListView.DragEnter += DragEnterEvent;
            albumListView.DragDrop += DragDropEvent;

            thumbnailsList = new ImageList();
            hidePanels();
            populateAlbumListView();

            addPhotoMutex = new Mutex();
        }
        ///<summary>
        ///This function is used to populate the thumbnails when an album is selected.
        ///created by Michael Flood Nov 7, 2011
        ///</summary>       
        private void populateAlbumListView()
        {
            //SuspendLayout();//wait to display the layout until after the list of albums is populated
            albumListView.Clear();// TODO review for Adam

            //add the "All Ohotos" album to the list of available albums
            albumListView.Items.Add(new ListViewItem("All Photos"));

            List<Album> albums = mainUserInterface.GetAlbumList();

            //display the available list of albums from the DB
            foreach (var album in albums)
            {
                ListViewItem item = new ListViewItem(album.AlbumName, 0);
                albumListView.Items.AddRange(new ListViewItem[] { item });
            }
            
            //ResumeLayout();//Display the list of albums in the albumListView listbox
        }

        /****************************************************************************************
         * When the "+" button is clicked the the newAlbum form is displayed and the list of albums
         * is listed within the listbox.
         * created by Michael Flood Nov 7, 2011
         * **************************************************************************************/
        private void addAlbumButton_Click(object sender, EventArgs e)
        {
            showNewAlbumPanel();
            populateAlbumListView();
        }

        /*****************************************************************************************
         * This function is used as an alternative to adding a new album via the tool strip at the 
         * top of the menu.
         * created by Michael Flood Nov 7, 2011
         * **************************************************************************************/
        private void createNewAlbumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showNewAlbumPanel();
            populateAlbumListView();
        }
        ///<summary>
        ///This function is used once the user selects the "+" button to  add a new album.
        ///created by Michael Flood Nov 7, 2011
        ///</summary>       
        private void createNewAlbumButton_Click(object sender, EventArgs e)
        {
            newAlbumNameTextBox.Focus();
            newAlbumNameTextBox.SelectAll();
            //if there is not a  name inputted into the "Name" tetbox then the user is presented with an error message
            if (newAlbumNameTextBox.Text.Length == 0)
            {
                DialogResult result = MessageBox.Show("No album name entered", "Please provide a name for the album.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (mainUserInterface.IsPresentAlbumByName(newAlbumNameTextBox.Text))
            {
                DialogResult result = MessageBox.Show("Album name already used.  Please choose another name.", "Album exists", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //add the album 
            mainUserInterface.AddAlbum(newAlbumNameTextBox.Text, newAlbumDescriptionTextBox.Text);
            hidePanels();//hide the "add album" screen
            newAlbumNameTextBox.Text = "";
            newAlbumDescriptionTextBox.Text = "";
            populateAlbumListView();
            albumListView.Items[albumListView.Items.Count-1].Selected = true; //keep the selected album the same
        }
        ///<summary>
        ///if the user chooses not to add the alum they can click on the "cancel" button
        ///created by Michael Flood Nov 7, 2011
        ///</summary>
        private void newAlbumCancelButton_Click(object sender, EventArgs e)
        {
            thumbnailListView.SelectedIndices.Clear();
            newAlbumNameTextBox.Text = "";//clear out the name box
            newAlbumDescriptionTextBox.Text = "";//clear out the description box
            hidePanels();//hide the box
        }

        ///<summary>
        ///This function is used to delete a photo from an album by using the "delete" key.
        ///created by Michael Flood Nov 7, 2011
        ///</summary>
        private void KeyDownDeleteAlbum(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Delete:
                    if (albumListView.SelectedIndices[0] == 0)
                    {
                        DialogResult result = MessageBox.Show("All Albums cannont be deleted.", "Please album to delete.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    deleteTheAlbum();
                    break;
            }
            
        }
        ///<summary>
        ///This is used to delete an album via the tool strip menu
        ///created by Michael Flood Nov 7, 2011
        ///</summary>
        private void deleteAlbumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //check to see if the user is attempting to delete the "All Photos" album, which is not allowed
            if (albumListView.SelectedIndices[0] == 0)
            {
                DialogResult result = MessageBox.Show("All Albums cannont be deleted.", "Please album to delete.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            deleteTheAlbum();
        }
        ///<summary>
        ///This function is used to deete an album selected by the user.
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void deleteTheAlbum()
        {
            //if no album is selected display an error message
            if (selectedAlbum == null)
            {
                DialogResult result = MessageBox.Show("No album selected", "Please album to delete.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //Ask the user to confirm their choice to delete the album
            DialogResult confirm = MessageBox.Show("Are you sure you want to delete the selected album: " + selectedAlbum.AlbumName, "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (confirm == DialogResult.No)
            {
                return;
            }

            mainUserInterface.DeleteAlbum(selectedAlbum.getAlbumID());
            populateAlbumListView();//after the album is deleted, display the new album list
            selectedAlbum = null;
            albumNameTextbox.Text = "";
            albumDescriptionTextBox.Text = "";
            hidePanels();
            albumListView.Items[0].Selected = true;
        }
        ///<summary>
        ///This function is used to add a photo to an album by clicking the "+" button
        ///created by Michael Flood, Nov 7, 2011
        ///</summary
        private void addPhotoToAlbumButton_Click(object sender, EventArgs e)
        {
            //allow the user to choose a picture(s) with the listed formats
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Pictures (*.jpg, *.jpeg, *bmp, *.png)| *.jpg; *.jpeg; *.bmp; *.png";
            open.Multiselect = true;
            open.ShowDialog();

            //if nothing us selected, return the function
            if (open.SafeFileNames.Count() == 0)
                return;

            //temporary list of albums
            List<Album> tlist = mainUserInterface.GetAlbumList();
            Album a = tlist[selectedAlbumIndex];

            //add the selected photo to the album
            for(int i = 0; i < open.SafeFileNames.Count(); i++)
            {
                addPhotoMutex.WaitOne();
                mainUserInterface.AddPhotoToAlbum(a.getAlbumID(), open.SafeFileNames.ElementAt(i), "", open.FileNames.ElementAt(i));
                addPhotoMutex.ReleaseMutex();
            }
            //populate the thumbnail panel with the photo
            PopulateThumbnailView(a);
        }
        /// <summary>
        /// This function identifies an album which the user has selected
        /// then calls the thumbnail view panel, or if clicked in white
        /// space, selects All Photos
        /// created by Michael Flood, Nov 7, 2011
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void albumSelectedView(object sender, EventArgs e)
        {
            if (selectedAlbumIndex != -1 && albumListView.Items[selectedAlbumIndex].Focused)
                return;

            if (albumListView.SelectedIndices.Count > 0)
            {
                if (albumListView.SelectedIndices[0] == 0) //check for all Photos
                {
                    selectedAlbum = mainUserInterface.GetUniquePhotoAlbum();
                    showViewThumbnailsPanel();
                    PopulateThumbnailView(selectedAlbum);
                    addToAlbumLabel.Text = selectedAlbum.AlbumName + " " + selectedAlbum.getPhotoList().Count;
                    albumDescriptionTextBox.Text = selectedAlbum.AlbumDescription;
                    addPhotoToAlbumButton.Enabled = false;
                    return;
                }

                addPhotoToAlbumButton.Enabled = true;

                selectedAlbumIndex = albumListView.SelectedIndices[0] -1;
                    
                List<Album> tlist = mainUserInterface.GetAlbumList();
                selectedAlbum = tlist[selectedAlbumIndex];
                
                showViewThumbnailsPanel();

                addToAlbumLabel.Text = tlist[selectedAlbumIndex].AlbumName + " " + selectedAlbum.getPhotoList().Count;
                PopulateThumbnailView(selectedAlbum);
                albumNameTextbox.Text = selectedAlbum.AlbumName;
                albumDescriptionTextBox.Text = selectedAlbum.AlbumDescription;
            }
            else
            {
                
                selectedAlbumIndex = -1;
                selectedAlbum = null;
                editAlbumNameTextBox.Text = "";
                editAlbumDescriptionTextBox.Text = "";
                albumNameTextbox.Text = "";
                albumDescriptionTextBox.Text = "";
        
            }

        }

        ///<summary>
        ///This function is used to check if there are ZERO albums in the albums view
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void checkForNoAlbumListViewIndicies(object sender, EventArgs e)
        {
            if (albumListView.SelectedIndices.Count == 0)
            {
                if(!albumListView.Items[0].Selected)
                albumListView.Items[0].Selected = true;
            }

        }

        ///<summary>
        ///This function is used to display the thumbnails of the selected album. Each thumbnail
        ///will be displayed after each time the user selects and album.
        ///modified Version 2.10 by Michael Flood, Nov 7, 2011
        ///Version 1.1 Modified by Ryan Hall 9-28-11
        ///Rewritten to account for new names, types and variables
        ///Version 1
        ///Created by Ryan Hall 9-25-11
        ///</summary>
        private void PopulateThumbnailView(Album selectedAlbum)
        {
            ImageList iList;
            // clear out the list
            thumbnailListView.Items.Clear();

             //do{
                 //Adam wants to put a progress bar here but hasn't gotten around to it
            // }while((iList = RyanCantCodeAnInterface.getThumbnailImageList()) == null);

            if (((iList = mainUserInterface.GetThumbnailImageList()) == null))
                return;

            thumbnailListView.LargeImageList = iList;

            //go through each photo in album
            foreach (Photo p in selectedAlbum.getPhotoList()) //and add the thumbnail to the list
            {
                ListViewItem item = new ListViewItem(p.PhotoName, 0);// create an item that is a row in listview
                item.ImageIndex = iList.Images.IndexOfKey(p.GetPhotoID().ToString());
                thumbnailListView.Items.Add(item);
            }

            ////thumbnailsList.Images.Clear();
            //List<Photo> photoList = albumid.getPhotoList();
            
            
            ////cycle through the photolist of the selected album
            //thumbnailsList.ImageSize = new Size(THUMBNAILSIZE, THUMBNAILSIZE); // set the size of the images
            //thumbnailsList.ColorDepth = ColorDepth.Depth32Bit;
       
            //foreach (Photo photo in photoList)
            //{
            //    ListViewItem item = new ListViewItem(photo.PhotoName, 0);// create an item that is a row in listview

            //    Image image;
            //    /*
            //    try
            //    {
            //     * */ //Adam Reaves Modified for higher quality and faster
            //    //get the fileDescriptor of the current photo
            //    image = Image.FromStream(photo.getFileDescriptor()); 
            //    thumbnailsList.Images.Add(image); // add the thumbnail to the list
            //    item.ImageIndex = thumbnailsList.Images.Count-1; // set the image index
            //   /*
            //    }
            //    catch
            //    {
            //        // skip it for now
            //    }
            //    */

            //    item.ImageIndex = thumbnailListView.Items.Count;
            //    thumbnailListView.Items.AddRange(new ListViewItem[] { item });
            //}
            
        }
        ///<summary>
        ///This is the function that handles the event when a user selects the option
        ///to view a slideshow.
        ///created by Joseph Alfred and Adam Reaves,Nov 7, 2011
        ///</summary>
        private void slideshowbutton_Click(object sender, EventArgs e)
        {  
            //If no photos are in the album then a messge is displayed and the slideshow
            //is not displayed
            if ((int) mainUserInterface.GetUniquePhotoListCount() == 0)
            {
                MessageBox.Show("Please add photos before using slideshow.", "No photos in album.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

          if (selectedAlbum != null && selectedAlbum.getPhotoList().Count > 0)
          {
                SlideShowForm presentSlideShow = new SlideShowForm(selectedAlbum);
                presentSlideShow.Show();
            }
        }
        ///<summary>
        ///This is the event handler for when a user chooses to delete a phot by pressing
        ///the "Delete" button.  Calls deleteThePhoto().
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void KeyDownDeletePhoto(object sender, KeyEventArgs e)
        {
            if (Keys.Delete != e.KeyCode)
                return;
            deleteThePhoto();
        }
        ///<summary>
        ///This is the event handler for when a user chooses to delete a phot by right
        ///mouse click "Delete Photo" selection.  Calls deleteThePhoto().
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void deletePhotoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            deleteThePhoto();
        }
        ///<summary>
        ///This function deletes selected photo/photos.
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void deleteThePhoto()
        {
            List<int> photoIdstoDelete = new List<int>();
            if (selectedAlbum.getAlbumID() == -1)
                return; //User is unable to delete from the all photos list

            
            //if they do not select a photo to delete then an error message will be displayed
            if (thumbnailListView.SelectedIndices.Count == 0)
            {
                MessageBox.Show("No Photos Selected", "Please Select a Photo to Delete.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //get the selected index of the photo(s) to be deleted
            for (int i = 0; i < thumbnailListView.SelectedIndices.Count; i++)
            {
                int pID = selectedAlbum.getPhotoList()[thumbnailListView.SelectedIndices[i]].GetPhotoID();
                photoIdstoDelete.Add(pID);
            }
            //confirm with the user that they want to delete the selected photos.
            foreach (int loc in photoIdstoDelete)
            {
                DialogResult confirm = MessageBox.Show("Are you sure you want to delete the selected photo: " + mainUserInterface.GetPhotoByID(loc).PhotoName, "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (confirm == DialogResult.Yes)
                    mainUserInterface.DeletePhoto(selectedAlbum.getAlbumID(), loc);
            }

            SuspendLayout();//wait to display the photos until after the thumbnails are loaded
            PopulateThumbnailView(selectedAlbum);
            ResumeLayout();
        }
        ///<summary>
        ///If the user is in the process of editing an album they will be able to cancel
        ///the changes that they have made
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void editAlbumCancelButton_Click(object sender, EventArgs e)
        {
            hidePanels();//display goes away and default " " values are assigned to the textboxes
            editAlbumNameTextBox.Text = "";
            editAlbumDescriptionTextBox.Text = "";
        }
        ///<summary>
        ///If the user chooses to edit a photo by using the toolstrip menu at the top
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void editPhotoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editPhoto();

        }
        ///<summary>
        ///if the user chooses to right-click a photo and then edit the photo
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        //private void editPhotoToolStripMenuItem1_Click(object sender, EventArgs e)
        //{
        //    editPhoto();
        //}
        ///<summary>
        ///This function is used to edit the selected photo within the album
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void editPhoto()
        {
            //if nothing is selected then an error message is displayed
            if (thumbnailListView.SelectedItems.Count == 0)
            {
                DialogResult result = MessageBox.Show("No photo selected", "Please select a photo.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //the user cannot select multiple photos to edit at one time
            if (thumbnailListView.SelectedItems.Count > 1)
            {
                DialogResult result = MessageBox.Show("More than one selected", "Please select only one photo.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                thumbnailListView.SelectedItems.Clear();
                return;
            }
            
            Photo tempP = selectedAlbum.getPhotoList()[thumbnailListView.SelectedIndices[0]];
            editPictureNameTextBox.Text = tempP.PhotoName;
            editPhotoDescriptionTextBox.Text = tempP.PhotoDescription;
            editPhotoPictureBox.Image = Image.FromStream(tempP.GetFileDescriptor());
            showEditPhotoPanel();
        }
        ///<summary>
        ///This function is used to edit an album. The user may edit the name, and description.
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void editAlbumButton_Click(object sender, EventArgs e)
        {
            int captureAlbumIndex = albumListView.SelectedIndices[0];
            editAlbumNameTextBox.Focus();
            editAlbumNameTextBox.SelectAll();

            //if there is no name entered then display the error message indicating so
            if (editAlbumNameTextBox.Text.Length < 1)
            {
                DialogResult result = MessageBox.Show("No name entered", "Please enter a name.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //modify the album
            mainUserInterface.ModifyAlbum(selectedAlbum.getAlbumID(), editAlbumNameTextBox.Text, editAlbumDescriptionTextBox.Text);
            //clear the textboxes and reset the thumbnail view
            editAlbumNameTextBox.Text = "";
            editAlbumDescriptionTextBox.Text = "";
            showViewThumbnailsPanel();
            populateAlbumListView();
            albumNameTextbox.Text = selectedAlbum.AlbumName;
            albumDescriptionTextBox.Text = selectedAlbum.AlbumDescription;
            albumListView.Items[captureAlbumIndex].Selected = true;
        }
        ///<summary>
        ///If the user chooses to cancel editing a photo the textboxes are reset and the photo is not changed
        ///crreated by Michael Flood, Nov 7, 2011
        ///</summary>
        private void cancelEditPhotoButton_Click(object sender, EventArgs e)
        {
            if (albumListView.SelectedItems.Count == 0)
                albumListView.Items[0].Selected = true;
            int captureAlbumIndex = albumListView.SelectedIndices[0];
            editPictureNameTextBox.Text = "";
            editPhotoDescriptionTextBox.Text = "";
            showViewThumbnailsPanel();
            albumListView.Items[captureAlbumIndex].Selected = true;
        }
        ///<summary>
        ///This button saves the changes made by the user to the photo name and/or description
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void saveEditPhotoButton_Click(object sender, EventArgs e)
        {
            editPictureNameTextBox.Focus();
            editPictureNameTextBox.SelectAll();

            //if no changes were made display an error message
            if (editPictureNameTextBox.Text.Length < 1)
            {
                DialogResult result = MessageBox.Show("No name entered", "Please enter a name.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //get the selected photoID then modify the photo and repopulate the thumbnail view
            int pID = selectedAlbum.getPhotoList()[thumbnailListView.SelectedIndices[0]].GetPhotoID();
            mainUserInterface.ModifyPhoto(pID, editPictureNameTextBox.Text, editPhotoDescriptionTextBox.Text);
            PopulateThumbnailView(selectedAlbum);
            showViewThumbnailsPanel();
        }
        ///<summary>
        ///Close the program by selecting the "Close" button from the top menu
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void closeProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        ///<summary>
        ///if the user double-clicks an inage in the thumbnail box then the photo is shown in fullscreen
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void thumnailListView_DoubleClick(object sender, EventArgs e)
        {
            photoFullScreen();
        }
        ///<summary>
        ///An alternate method for the user so that they can view a larger image of the selected photo
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void viewPhotoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            photoFullScreen();
        }
        ///<summary>
        ///This function is used to get the selected photoID and launches a new form to display
        ///the photo in a larger view.
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void photoFullScreen()
        {
            if (thumbnailListView.SelectedItems.Count == 0)
            {
                DialogResult result = MessageBox.Show("Please select a photo to view.", "No Photo Selected", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Photo pID = selectedAlbum.getPhotoList()[thumbnailListView.SelectedIndices[0]];
            SinglePhotoViewForm photoViewForm = new SinglePhotoViewForm(pID);
            photoViewForm.Show();
            photoViewForm.BringToFront();
        }
        ///<summary>
        ///this function is used to 'hide' un-used panels and show the newAlbum Panel
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void showNewAlbumPanel()
        {
            SuspendLayout();
            newAlbumEntryPanel.Visible = true;
            newAlbumEntryPanel.Enabled = true;
            newAlbumEntryPanel.BringToFront();
            editPhotoPanel.Visible = false;
            editPhotoPanel.Enabled = false;
            viewThumbnailsPanel.Visible = false;
            viewThumbnailsPanel.Enabled = false;
            editAlbumPanel.Visible = false;
            editAlbumPanel.Enabled = false;
            ResumeLayout();
        }
        ///<summary>
        ///This function is used to 'hide' un-used panels and display the editAlbum panel
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void showEditAlbumPanel()
        {
            SuspendLayout();
            newAlbumEntryPanel.Visible = false;
            newAlbumEntryPanel.Enabled = false;
            editPhotoPanel.Visible = false;
            editPhotoPanel.Enabled = false;
            viewThumbnailsPanel.Visible = false;
            viewThumbnailsPanel.Enabled = false;
            editAlbumPanel.Visible = true;
            editAlbumPanel.Enabled = true;
            editAlbumPanel.BringToFront();
            ResumeLayout();
        }
        ///<summary>
        ///This function is used to 'hide' the un-used panels and display the thumbnail panel
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void showViewThumbnailsPanel()
        {
            SuspendLayout();
            newAlbumEntryPanel.Visible = false;
            newAlbumEntryPanel.Enabled = false;
            editPhotoPanel.Visible = false;
            editPhotoPanel.Enabled = false;
            viewThumbnailsPanel.Visible = true;
            viewThumbnailsPanel.Enabled = true;
            viewThumbnailsPanel.BringToFront();
            editAlbumPanel.Visible = false;
            editAlbumPanel.Enabled = false;
            ResumeLayout();
        }
        ///<summary>
        ///This function is used to 'hide' un-used panels and display the editPhoto panel
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void showEditPhotoPanel()
        {
            SuspendLayout();
            newAlbumEntryPanel.Visible = false;
            newAlbumEntryPanel.Enabled = false;
            editPhotoPanel.Visible = true;
            editPhotoPanel.Enabled = true;
            editPhotoPanel.BringToFront();
            viewThumbnailsPanel.Visible = false;
            viewThumbnailsPanel.Enabled = false;
            editAlbumPanel.Visible = false;
            editAlbumPanel.Enabled = false;
            ResumeLayout();
        }
        ///<summary>
        ///This function is sed to hide all panels, used in the opening of the application
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void hidePanels()
        {
            SuspendLayout();
            newAlbumEntryPanel.Visible = false;
            newAlbumEntryPanel.Enabled = false;
            editPhotoPanel.Visible = false;
            editPhotoPanel.Enabled = false;
            viewThumbnailsPanel.Visible = false;
            viewThumbnailsPanel.Enabled = false;
            editAlbumPanel.Visible = false;
            editAlbumPanel.Enabled = false;
            ResumeLayout();
        }
        ///<summary>
        ///This function is used to allow the user to edit an album from the menu strip.
        ///The "All Albums" album is NOT allowed to be modified by the user
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void editAlbumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (albumListView.SelectedItems.Count == 0)
            {
                DialogResult result = MessageBox.Show("No album selected.", "Please select an album to edit.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //if the "All Photos" album is selected an error message is displayed
            if (albumListView.SelectedIndices[0] == 0)
            {
                DialogResult result = MessageBox.Show("All Albums cannont be edited.", "Please select an album to edit.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            editTheAlbum();
        }
        ///<summary>
        ///This is the right-click alternate to editing an album. The user is NOT able to
        ///edit the "All Albums" album
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        //private void editAlbumToolStripMenuItem1_Click(object sender, EventArgs e)
        //{
        //    if (albumListView.SelectedItems.Count == 0)
        //    {
        //        DialogResult result = MessageBox.Show("No album selected.", "Please select an album to edit.", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }
        //    //if the user attempts to edit the "All Albums" an error message is displayed
        //    if (albumListView.SelectedIndices[0] == 0)
        //    {
        //        DialogResult result = MessageBox.Show("All Albums cannont be edited.", "Please select an album to edit.", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }
        //    editTheAlbum();
        //}
        ///<summary>
        ///This is the main function for editing an album.
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void editTheAlbum()
        {
            //if there is no album selected then an error message is displayed indicating so
            if (selectedAlbum == null)
            {
                DialogResult result = MessageBox.Show("No album selected", "Please album to edit.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            showEditAlbumPanel();//display the editAlbumPanel
            editAlbumNameTextBox.Text = selectedAlbum.AlbumName;//change the album name
            editAlbumDescriptionTextBox.Text = selectedAlbum.AlbumDescription;//change the album description
        }
        ///<summary>
        ///This is the main function for displaying the "About Us" box
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BAAboutBox aboutBox = new BAAboutBox();
            aboutBox.ShowDialog();
        }
       ///<summary>
        ///This is the function that allows the user to view the "Users Guide" 
        ///This requires that the user have Adobe Reader installed
        ///created by Joseph Alfred, Nov 7, 2011
       ///</summary>
        private void userManualToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            { //Create a link to the user manual and start the process
                System.Diagnostics.Process.Start(@"UserManual(Cycle2).pdf");
            }
            catch
            { 

            }
        }

        //DRAG AND DROP (a Mike Flood solo)
        ///<summary>
        ///This function is used to begin the drag and drop event, when the user mouses-down on the image
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void StartDragEvent(object sender, ItemDragEventArgs e)  //this function works.  It captures the photos to drag.
        {
            if (e.Button == MouseButtons.Left)
            {
                List<Photo> selPhotoList = new List<Photo>();
                //get the list of photos selected by the user
                for (int i = 0; i < thumbnailListView.SelectedItems.Count; i++)
                {
                    int pID = selectedAlbum.getPhotoList()[thumbnailListView.SelectedIndices[i]].GetPhotoID();
                    selPhotoList.Add(selectedAlbum.getPhotobyID(pID));
                }
                //copy the selected item to the list
                albumListView.DoDragDrop(selPhotoList, DragDropEffects.Copy);
            }
            return;
        }
       ///<summary>
        ///This is a copy function from c#
        ///created by Michael Flood, Nov 7, 2011
       ///</summary>
        private void DragEnterEvent(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        ///<summary>
        ///This function copies the list of selected items to the point at which the user
        ///'drops' them into
        ///created by Michael Flood, Nov 7, 2011
        ///</summary>
        private void DragDropEvent(object sender, DragEventArgs e)
        {
            List<Photo> dragCopyList = (List<Photo>)e.Data.GetData(typeof(List<Photo>));//this function works. It copies the list of dragged photos.

            Point cp = albumListView.PointToClient(new Point(e.X, e.Y));
            ListViewItem dragToItem = albumListView.GetItemAt(cp.X, cp.Y);
            int dropIndex = dragToItem.Index;

            //the user may not be allowed to place items into the "All Albums" album
            if (dropIndex == 0)
            {
                DialogResult result = MessageBox.Show("All Photos cannont accept drag and drop.", "No album selected.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //get the album list to drop the selected photos into
            List<Album> tlist = mainUserInterface.GetAlbumList();
            Album targetAlbum = tlist[dropIndex-1];

            //the user may not drop selected pictures into the same album they were selected from
            if (targetAlbum == selectedAlbum)
            {
                DialogResult result = MessageBox.Show("Albums are the same, please select a differnt album to drag to.", "Same Album", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //for each photo in the selectedList, copy them into the chosen album
            foreach (Photo p in dragCopyList)
            {
                mainUserInterface.DragPhotosToAlbum(targetAlbum.getAlbumID(), p.GetPhotoID());
            }
        }        
    }
    /*
     * using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BadassPhotoAlbum
{

    /// <summary>
    /// The main form for the program, will hold child forms
    /// Contains a menu bar to close the program, create a new album
    /// 
    /// Version 1.1 Ryan Hall 9-22-11
    /// Added events to handle create album button to open albumviewform
    /// Added events to openselected album
    /// Version 1
    /// Created By Ryan Hall 9-22-11
    /// </summary>
    public partial class MainForm : Form
    {
        private int LEFT = 15; // where to load forms in main
        private int TOP = 15;

        //NewAlbumForm, AlbumSelectionForm here so that only one can be opened at a time
        NewAlbumForm newAlbumForm;
        AlbumSelectionForm albumSelectionForm;
        List<AlbumViewForm> albumViewFormList; // holds all open albums
        protected ProgramDb programDb; // holds the information from the xml files

        /// <summary>
        /// Constructor for MainForm
        /// creates and loads the program database
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-22-11
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            // load the database
            programDb = new ProgramDb();
            albumViewFormList = new List<AlbumViewForm>();
        }

        /// <summary>
        /// open default form
        /// 
        /// Version 1.01 edited by Ryan Hall 9-24-11
        /// added tempAlbumViewFrom so Ethan can build off it
        /// Version 1
        /// Created by Ryan Hall 9-23-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            // load the open album form
            openAlbumToolStripMenuItem_Click(sender, e);
        }

        /// <summary>
        /// closes the program
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-23-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// opens a form to select an album
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-23-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openAlbumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // if statements to have only one instance of the form
            if (albumSelectionForm == null)
            {
                // show the form only once
                albumSelectionForm = new AlbumSelectionForm(programDb, windowToolStripMenuItem);

                // subscribe to events
                albumSelectionForm.RaiseCustomEvent += HandleCreateAblumButtonClick;
                albumSelectionForm.RaiseOpenEventHandler += HandleOpenAlbumButtonClick;
                albumSelectionForm.MdiParent = this;
                try
                {
                    albumSelectionForm.Show(); // may throw an error if album view form is open when program closes
                }
                catch
                {
                    Application.Exit();
                }
                

                // load the form in the upper left corner
                albumSelectionForm.Location = new Point(LEFT, TOP);
            }
            else if (albumSelectionForm.IsDisposed == true)
            {
                albumSelectionForm = null;
                // recall this method to create a new instance of the window
                openAlbumToolStripMenuItem_Click(sender, e);
            }
            else if (albumSelectionForm.WindowState == FormWindowState.Minimized || albumSelectionForm.WindowState == FormWindowState.Normal)
            {
                albumSelectionForm.WindowState = FormWindowState.Normal;
                albumSelectionForm.Location = new Point(LEFT, TOP);
            }
            
                
        }

        /// <summary>
        /// handles an event to launch a new album creation form
        /// 
        /// to do
        /// fires too many times, too fast
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-23-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleCreateAblumButtonClick(object sender, CreateAlbumClickEvent e)
        {
            createNewAlbumToolStripMenuItem_Click(sender, e);
        }

        /// <summary>
        /// launches a NewAlbumForm
        /// 
        /// will be launched by a future event
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-23-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void createNewAlbumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (newAlbumForm == null)
            {
                newAlbumForm = new NewAlbumForm(programDb, windowToolStripMenuItem);

                newAlbumForm.MdiParent = this;
                newAlbumForm.Show();

                // future open ablumview event
                newAlbumForm.RaiseOpenEventHandler += HandleOpenAlbumButtonClick;

                // load the form in the upper left corner
                newAlbumForm.Location = new Point(LEFT, TOP);
            }
            else if (newAlbumForm.IsDisposed == true) // if the form was closed
            {
                newAlbumForm = null;
                // recall this method to create a new instance of the window
                createNewAlbumToolStripMenuItem_Click(sender, e);
            }
            else if (newAlbumForm.WindowState == FormWindowState.Minimized || newAlbumForm.WindowState == FormWindowState.Normal) // if the form was minimized
            {
                newAlbumForm.WindowState = FormWindowState.Normal;
                newAlbumForm.Location = new Point(LEFT, TOP);
            }
        }

        /// <summary>
        /// Handles an open album event, only allow an album to be opened once
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-25-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleOpenAlbumButtonClick(object sender, OpenAlbumEvent e)
        {
            // search if the album is already open, if it is focus
            foreach (var a in albumViewFormList)
            {
                if (a.FormAlbum != null)
                {
                    if (e.Album.PrimaryKey == a.FormAlbum.Properties.PrimaryKey) // focus on this album
                    {
                        a.windowMenuItem_Click(sender, e);
                        return;
                    }
                }
            }

            // else 
            // create the new albumViewForm to be used
            AlbumViewForm album = new AlbumViewForm(programDb, e.Album, windowToolStripMenuItem);
            album.MdiParent = this;
            album.Show();
            album.Location = new Point(LEFT, TOP);
            album.RaiseCloseEventHandler += HandleAlbumViewCloseEvent; // subscribe to the event
            album.RaiseBackToSelectionEventHandler += HandleBackToSelectionEvent; // subscribe to event

            // add the album to the container
            albumViewFormList.Add(album);
        }

        /// <summary>
        /// Handles a close album event
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-25-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleAlbumViewCloseEvent(object sender, CloseAlbumViewEvent e)
        {
            // debugging help
            //MessageBox.Show("Window: " + e.Album.Name, "Closed Window", MessageBoxButtons.OK);
            foreach (var a in albumViewFormList)
            {
                if (a.FormAlbum != null)
                {
                    if (e.Album.PrimaryKey == a.FormAlbum.Properties.PrimaryKey) // focus on this album
                    {
                        albumViewFormList.Remove(a); // remove the album from the list
                        break; // should no longer be in the list
                    }
                }
            }
            // if there are no more open albums
            if (albumViewFormList.Count == 0)
            {
                openAlbumToolStripMenuItem_Click(sender, e); // open the albumSelectionForm
            }
        }

        /// <summary>
        /// Handles a BackToSelectionEvent
        /// Form does not close
        /// 
        /// Version 1
        /// Created by Ryan Hall 9-26-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleBackToSelectionEvent(object sender, EventArgs e)
        {
            openAlbumToolStripMenuItem_Click(sender, e);
        }
    }
}

     * */
}
